package com.aliyun.messagemgt.convert;

import com.alibaba.algo.cooperation.feign.vo.req.*;
import com.alibaba.algo.cooperation.feign.vo.res.*;
import com.aliyun.messagemgt.common.dto.foura.*;
import com.github.pagehelper.PageInfo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Mapper
public interface FouraConverter {
    FouraConverter INSTANCE = Mappers.getMapper(FouraConverter.class);

    List<FouraGetFirstLevelOrgDTO> feignGetFirstLevelOrgResponseVOList2fouraGetFirstLevelOrgDTOList(List<FeignGetFirstLevelOrgResponseVO> feignGetFirstLevelOrgResponseVOList);

    List<FouraOrgInfoDTO> orgInfoResVOList2fouraOrgInfoDTOList(List<OrgInfoResVO> orgInfoResVOList);

    List<FouraOrgUserInfoDTO> orgUserInfoResVO2fouraOrgUserInfoDTOList(List<OrgUserInfoResVO> orgUserInfoResVOList);

    @Mappings({
                @Mapping(source = "ORG_NAME",target = "orgName")
            })
    FouraOrgUserInfoDTO orgUserInfoResVO2fouraOrgUserInfoDTO(OrgUserInfoResVO orgUserInfoResVO);

    @Mappings({
            @Mapping(source = "keyword",target = "searchKey"),
            @Mapping(expression = "java(processOrgIdList(fouraGetUserByOrgQry))",target = "orgIdList"),
    })
    SelectOrgUserReqVO fouraGetUserByOrgQry2selectOrgUserReqVO(FouraGetUserByOrgQry fouraGetUserByOrgQry);

    default List<String> processOrgIdList(FouraGetUserByOrgQry fouraGetUserByOrgQry){
        List<String> orgIdList = Optional.ofNullable(fouraGetUserByOrgQry.getOrgIdList()).orElseGet(() -> new ArrayList<>());
        Long orgId = fouraGetUserByOrgQry.getOrgId();
        if(Objects.nonNull(orgId)){
            orgIdList.add(orgId.toString());
        }
        return orgIdList.stream().distinct().collect(Collectors.toList());
    }

    OrgInfoResVO fouraGetOrgListByParentIdQry2orgInfoResVO(FouraGetOrgListByParentIdQry fouraGetOrgListByParentIdQry);

    @Mappings({
            @Mapping(source = "pageNum",target = "pageIndex"),
            @Mapping(source = "keyword",target = "searchKey"),
    })
    FeignSelectRoleListRequestVO fouraPageRoleQry2feignSelectRoleListRequestVO(FouraPageRoleQry pageQry);

    PageInfo<FouraRoleDTO> feignRoleListResponseVOPageInfo2fouraRoleDTOPageInfo(PageInfo<FeignRoleListResponseVO> feignRoleListResponseVOPageInfo);

    List<FouraPositionUserInfoDTO> positionUserInfoResVOList2fouraPositionUserInfoDTO(List<PositionUserInfoResVO> positionUserInfoResVOList);

    SelectPositionUserReqVO fouraPositionUserQry2selectPositionUserReqVO(FouraPositionUserQry qry);

    SelectUserListByRoleIdListReqVO fouraRoleUserQry2selectUserListByRoleIdListReqVO(FouraRoleUserQry qry);

    List<FouraRoleUserInfoDTO> rpcUserInfoVOList2fouraRoleUserInfoDTOList(List<RpcUserInfoVO> rpcUserInfoVOList);

    SelectUserListInfoReqVo fouraUserQry2selectUserListInfoReqVo(FouraUserQry qry);

    List<FouraUserInfoDTO> rpcUserInfoVOList2fouraUserInfoDTOList(List<RpcUserInfoVO> rpcUserInfoVOList);

    List<FouraUserInfoDTO> fouraOrgUserInfoDTOS2fouraUserInfoDTOS(List<FouraOrgUserInfoDTO> fouraOrgUserInfoDTOS);

    List<FouraUserInfoDTO> fouraPositionUserInfoDTOS2fouraUserInfoDTOS(List<FouraPositionUserInfoDTO> fouraPositionUserInfoDTOS);

    List<FouraUserInfoDTO> fouraRoleUserInfoDTOS2fouraUserInfoDTOS(List<FouraRoleUserInfoDTO> fouraRoleUserInfoDTOS);
}
